#include "re_chck.h"
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <math.h>

//TODO: use src and dst two buffers
INT32 recover_jt808msg(INT8 *pDat, INT32 datLen)
{
	INT32 ix = 0;
	INT32 jx = 0;
	INT8 msg[MAX_PKG_LEN*2] = {0};

	for(ix = 1; ix < datLen - 1; ix++){
		if(pDat[ix] == 0x7d && pDat[ix+1] == 0x02){
			msg[++jx] = 0x7e;
			ix++;
		}
		else if(pDat[ix] == 0x7d && pDat[ix+1] == 0x01){
			msg[++jx] = 0x7d;
			ix++;
		}else{
			msg[++jx] = pDat[ix];
		}
	}
	msg[0] = 0x7e;
	msg[jx+1] = 0x7e;
	memcpy(pDat, msg, jx+2);

	return (jx+2);
}

INT32 check_crc(UINT8 *prDat, INT32 datLen)
{
	INT32 ix;
	UINT8 *pTmp = prDat;
	UINT8 ch_byte = 0, ch_crc = 0;

	ch_byte = pTmp[1];

	for(ix = 2; ix < datLen - 2; ix++){
		ch_byte ^= pTmp[ix];
	}

	ch_crc = pTmp[ix];

	if(ch_byte != ch_crc){
		E(" Check code error: %x != %x\n", ch_byte, ch_crc);
		return -1;
	}
	E(" Check code right.");
	return 0;
}

UINT8 count_crc(UINT8 *pDat, INT32 datLen)
{
	INT32 ix;
	UINT8 *pTmp = pDat;
	UINT8 crc = 0;

	crc = (UINT8)pTmp[1];
	for(ix = 2; ix < datLen; ix++){
		crc ^= (UINT8)pTmp[ix];
	}

	return crc;
}

INT32 disconnect_judge(INT32 fd, INT32 tm)
{
	fd_set rfds;
	struct timeval tv;
	INT32 r;

	FD_ZERO(&rfds);
	FD_SET(fd, &rfds);

	tv.tv_sec = tm;
	tv.tv_usec = 0;
	r = select(fd+1, &rfds, NULL, NULL, &tv);
	if(r == -1){
		perror("select().");
		exit(1);
	}
	else if(r == 0)
	{
		E(" Timeout no data recived.");
		return -1;
	}
	
	if(FD_ISSET(fd, &rfds))
		return 0;
	else 
		return -1;
}

INT32 time_convert(UINT8 *pIn, INT8 *pOut)
{
	INT8 pTmp[20] = {0}, pMsg[20] = {0};
	INT8 *p = &pMsg[2];
	INT32 ix = 0;

	if(pIn == NULL || pOut == NULL)
		return -1;
	memcpy(pMsg, "20", 2);

	while(*pIn){
		sprintf(&pTmp[ix], "%02x", *pIn++);
		ix += 2;
	}

	for(ix = 0; ix < sizeof(pTmp)-1; ix++ ){
		if(ix == 2 || ix == 4)
			*p++ = '-';
		else if(ix == 6)
			*p++ = ' ';
		else if(ix == 8 || ix == 10)
			*p++ = ':';
		*p++ = pTmp[ix];
	}
	//memcpy(pOut, pMsg, strlen(pMsg));
	memcpy(pOut, pMsg, 19);
	
	return 0;
}

DWORD create_auth(void)
{
	DWORD rand_num;

	srand(time(NULL));
	rand_num = rand()%(MAX_NUM - MIN_NUM) + MIN_NUM + MAGIC_NUM;

	return rand_num;
}

INT32 simHex_convert_str(UINT8 *pIn, INT8 *pOut)
{
	INT32 ix;
	if(pIn == NULL || pOut == NULL)
		return -1;

	//for(ix = 0; ix < strlen((const char *)pIn); ix++){
	for(ix = 0; ix < 6; ix++){
		sprintf(pOut, "%02x", pIn[ix]);
		pOut += 2;
	}
	return 0;
}

INT32 simStr_convert_hex(INT8 *pIn, UINT8 *pOut)
{
	INT8 tmp[4];
	INT32 r, ix = 0;

	if(pIn == NULL || pOut == NULL)
		return -1;

	while(*pIn){
		memset(tmp, 0, sizeof(tmp));
		memcpy(tmp, pIn, 2);
		r = strtoul((const char *)tmp, NULL, 10);

		memset(tmp, 0, sizeof(tmp));
		sprintf(tmp, "%x", r);
		r = strtoul((const char *)tmp, NULL, 16);

		pOut[ix++] = r;
		pIn += 2;
	}
	return 0;	
}

INT32 str_to_hex(INT8 *pIn)
{
	INT8 tmp[11] = {0};
	INT32 vale = 0;

	vale = strtoul((const char *)pIn, NULL, 10);
	sprintf(tmp, "%x", vale);
	vale = strtoul((const char *)tmp, NULL, 16);

	return vale;	
}

INT32 strFloat_to_hex(INT8 *pIn, INT32 num)
{
	INT8 tmp[11] = {0};
	float f = 0.0;
	INT32 vale;

	f = strtod(pIn, NULL);
	vale = f*pow(10, num);
	sprintf(tmp, "%x", vale);
	vale = strtoul((const char *)tmp, NULL, 16);

	return vale;
}

